package homework.Integrated;

interface LibraryOperations {
    boolean borrowBook(String bookId);    // 借书
    boolean returnBook(String bookId);   // 还书
}

class Library implements LibraryOperations {
    private Book[] books;    // 图书数组
    private int count;       // 当前图书数量

    public Library(int capacity) {
        books = new Book[capacity];
        count = 0;
    }

    public void addBook(Book book) {
        if (count < books.length) {
            books[count++] = book;
        } else {
            System.out.println("图书库已满，无法添加新图书。");
        }
    }

    @Override
    public boolean borrowBook(String bookId) {
        for (int i = 0; i < count; i++) {
            if (books[i].getId().equals(bookId) && !books[i].isBorrowed()) {
                books[i].setBorrowed(true);
                System.out.println("图书 " + books[i].getTitle() + " 借阅成功。");
                return true;
            } else if (books[i].getId().equals(bookId) && books[i].isBorrowed()) {
                System.out.println("图书 " + books[i].getTitle() + " 已被借阅。");
                return false;
            }
        }

        System.out.println("未找到该书");
        return false;
    }
    @Override
    public boolean returnBook(String bookId) {
        for (int i = 0; i < count; i++) {
            if (books[i].getId().equals(bookId) && books[i].isBorrowed()) {
                books[i].setBorrowed(false);
                System.out.println("图书 " + books[i].getTitle() + " 归还成功。");
                return true;
            } else if (books[i].getId().equals(bookId) && !books[i].isBorrowed()) {
                System.out.println("图书 " + books[i].getTitle() + " 未被借阅。");
                return false;
            }
        }
        System.out.println("未找到该书");
        return false;
    }

    public void displayAllBooks() {
        System.out.println("===图书馆藏书状态===");
        for (int i = 0; i < count; i++) {
            System.out.println(books[i]);
        }
        System.out.println("================");
    }
}